package com.kx.hackathon.service.impl;

import com.kx.hackathon.service.HandlerService;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service("AiQaServiceImpl")
public class AiQaServiceImpl implements HandlerService {

    @Autowired
    OkHttpClient httpClient;

    public String chat(String word) throws Exception {
        String url = "https://api.ownthink.com/bot?appid=9ffcb5785ad9617bf4e64178ac64f7b1&spoken="+word;
        System.out.println(url);
//        FormBody.Builder params = new FormBody.Builder();
//        params.add("question",word);
        Request request = new Request.Builder()
                .url(url)
                .header("X-Requested-With","XMLHttpRequest")
//                .post(params.build())//默认就是GET请求，可以不写
                .build();
        Response response = httpClient.newCall(request).execute();
        String json = response.body().string();
        String res = json.split("\"text\": \"")[1].split("\"")[0].replaceAll("小思","Bobo");
        System.out.println(res);
        return res;
    }

    @Override
    public String handle(String msg, String arg) throws Exception {
        return chat(msg);
    }

    @Override
    public String init(String msg, String arg) {
        return null;
    }

    @Override
    public boolean needInit() {
        return false;
    }
}
